using System;
using System.Collections.Generic;
using System.Data;

using Elev.Core.DAL;
using Elev.Core.Utility;

namespace Elev.Core
{
    public class ExamsFactory
    {
        public List<Exam> GetExams()
        {
            ExamInternal e = new ExamInternal();
            DataTable dt = e.GetExams();
            List<Exam> list = new List<Exam>();

            if (dt.Rows.Count > 0)
                foreach (DataRow dr in dt.Rows)
                    list.Add(new Exam(dr));

            list.Sort(new ExamDateComparer());
            return list;
        }

        public List<Exam> GetExams(int PeriodID)
        {
            ExamInternal e = new ExamInternal();
            DataTable dt = e.GetExams(PeriodID);
            List<Exam> list = new List<Exam>();

            if (dt.Rows.Count > 0)
                foreach (DataRow dr in dt.Rows)
                    list.Add(new Exam(dr));

            list.Sort(new ExamDateComparer());
            return list;
        }

        public Exam GetExam(int ID)
        {
            ExamInternal e = new ExamInternal();
            DataTable dt = e.GetExam(ID);

            if (dt.Rows.Count == 0)
                throw new ArgumentException(string.Format("There is no exam for the id '{0}'", ID));

            if (dt.Rows.Count > 1)
                throw new ArgumentException(string.Format("There is more than one exam for the id '{0}'", ID));

            return new Exam(dt.Rows[0]);
        }
    }
}
